$(document).ready(function() {
    $('.oder_details ul li').click(function() {
        $(this).addClass("oder_details_active");
        $(this).siblings().removeClass("oder_details_active");
        // 隐藏所有 打开对应选项卡
        $('.orders').hide();
        $('.' + $(this).attr('name')).show();
        a();
    });
    a();
});


$(function() {
    $("#myinfo").click(function() {
        window.location.href = '../my/me.html';
    });

    var token = localStorage.phone;
    /*console.log(token);*/
    $.ajax({
        url: "https://newapp.icandrive.cn/api/studentorder.php",
        type: 'post',
        dataType: 'json',
        data: {
            token: token,
            status: status
        },
        success: function(data) {
            var now = new Date();
            var hour = now.getHours();
            var minu = now.getMinutes();
            var right_time = hour + minu / 60;
            if(data.status == 200) {

                $(data.conduct['test']).each(function() {
                    var str = Date.parse(new Date()) / 1000;
                    var timestamp = Date.parse(this.order_time) / 1000;
                    var date = (str - timestamp) / 3600 / 24;
                    var order_id = this.order_id;
                    var time_id = this.time_id;
                    var button = '<button onclick="cancel(' + order_id + ',' + date + ',' + time_id + ')">确认取消</button>';
                    if(date > 0) {
                        var a = timeF(this.time_id);
                        if(right_time < a[1] && right_time > a[0]) {
                            button = '<button onclick="doing(' + date + ')">进行中</button>';
                        }
                        if(right_time > a[1]) {
                            button = '<button onclick="com(' + order_id + ',' + date + ')">评价</button>';
                            localStorage.pingjiaorderid = order_id;
                        }
                    }

                    var time = this.order_time.substring(5, 10);

                    if(this.order_type == 'test') {
                        this.order_type = '约教'
                    } else {
                        this.order_type = '送考'
                    }
                    if(this.subject == 'two') {
                        this.subject = '二'
                    } else {
                        this.subject = '三'
                    }
                    $("#text").append("<div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>" + time + "</li></ul></li><li class='head_portrait float_li1'><img  src=" + this.teacher_photo + " ><p>" + this.teacher_name + "</p></li><li class='subject_reason float_li1'><h3>科" + this.subject + "培训</h3><span id='time'>" + this.time_name + "</span><p>" + this.meet_address + "</p></li></ul></div></div><div class='btn_list clearfix'><button class='tel' tel='" + this.phone + "'>电话联系</button>" + button + "</div><input  class='canclew' type='hidden' name=" + this.user_id + " teacher=" + this.teacher_id + ">");


                });
                $(data.conduct['shuttle']).each(function() {
                    var str = Date.parse(new Date()) / 1000;
                    var timestamp = Date.parse(this.order_time) / 1000;
                    var date = (str - timestamp) / 3600 / 24;
                    var order_id = this.order_id;
                    var time_id = this.time_id;
                    var button = '<button onclick="cancel(' + order_id + ',' + date + ',' + time_id + ')">确认取消</button>';
                    if(date > 0) {
                        var a = timeF(this.time_id);
                        if(right_time < a[1] && right_time > a[0]) {
                            button = '<button>进行中</button>';
                        }
                        if(right_time > a[1]) {
                            button = '<button onclick="com(' + order_id + ')">评价</button>';
                        }
                    }
                    var time = this.order_time.substring(5, 10);
                    if(this.order_type == 'test') {
                        this.order_type = '约教'
                    } else {
                        this.order_type = '送考'
                    }
                    if(this.subject == 'two') {
                        this.subject = '二'
                    } else {
                        this.subject = '三'
                    }
                    $("#shuttle").append(" <div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>" + time + "</li></ul></li><li class='head_portrait float_li1'><img src=" + this.teacher_photo + " ><p>" + this.teacher_name + "</p></li><li class='subject_reason float_li1'><h3>科" + this.subject + this.order_type + "</h3><span id='time'>" + this.time_name + "</span><p>" + this.meet_address + "</p></li></ul></div></div><div class='btn_list clearfix'><button class='tel' tel='" + this.phone + "'>电话联系</button>" + button + "</div>");


                });

                $(data.clear['test']).each(function() {
                    var time = this.order_time.substring(5, 10);
                    if(this.order_type == 'test') {
                        this.order_type = '约教'
                    } else {
                        this.order_type = '送考'
                    }
                    if(this.subject == 'two') {
                        this.subject = '二'
                    } else {
                        this.subject = '三'
                    }
                    $("#can").append("<div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>" + time + "</li></ul></li><li class='head_portrait float_li1'><img src=" + this.teacher_photo + " ><p>" + this.teacher_name + "</p></li><li class='subject_reason float_li1'><h3>科" + this.subject + this.order_type + "</h3><span>" + this.time_name + "</span><p>" + this.cancle + "</p></li></ul></div></div><div class='btn_list clearfix'><button class='tel' tel='" + this.phone + "'>电话联系</button><button class='report' type=" + this.order_id + ">举报</button></div><input class='teacher_id' type='hidden' name=" + this.teacher_id + ">");
                    $(".report").click(function() {
                        var order_id = $(".report").attr('type');
                        localStorage.order_id = order_id;
                        window.location.href = "../report/report_reason.html";
                    });

                });
                $(data.clear['shuttle']).each(function() {
                    var time = this.order_time.substring(5, 10);
                    if(this.order_type == 'test') {
                        this.order_type = '约教'
                    } else {
                        this.order_type = '送考'
                    }
                    if(this.subject == 'two') {
                        this.subject = '二'
                    } else {
                        this.subject = '三'
                    }
                    $("#can").append("<div class='orders  orders1' id='can'><div class='oder_canceled'><p>学员取消的订单</p></div><div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>" + time + "</li></ul></li><li class='head_portrait float_li1'><img src=" + this.teacher_photo + " ><p>" + this.teacher_name + "</p></li><li class='subject_reason float_li1'><h3>科" + this.subject + this.order_type + "</h3><span>" + this.time_name + "</span><p>" + this.cancle + "</p></li></ul></div></div><div class='btn_list clearfix'><button class='tel' tel='" + this.phone + "'>电话联系</button><button>举报</button></div></div>");
                });
                $(data.complete).each(function() {
                    //console.log(data.complete);
                    var time = this.order_time.substring(5, 10);
                    if(this.order_type == 'test') {
                        this.order_type = '约教'
                    } else {
                        this.order_type = '送考'
                    }
                    if(this.subject == 'two') {
                        this.subject = '二'
                    } else {
                        this.subject = '三'
                    }


                    var str="<p>" + this.assess + "</p>";
                    if(this.assess == null){
        		 	str="<p onclick='no_pingjia("+this.order_id+")'>评价</p>";

               		 }

                    $(".orders3").append("<div class='cancle_oders_list' id='complete'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>" + time + "</li><li>16:40</li></ul></li><li class='head_portrait float_li1'><img src=" + this.teacher_photo + " ><p>" + this.teacher_name + "</p></li><li class='subject_reason float_li1'><h3>科" + this.subject + this.order_type + "</h3><span>" + this.time_name + "</span>"+str+"</li></ul></div></div>");

                });
                $('.tel').click(function() {
                    window.location.href = "tel:" + $(this).attr('tel');
                })

            }
            if(data.status == 250) {
                layer.msg("请重新登录")
                setInterval(function() {
                    window.location.href = "../login/log_in.html";
                }, 2000);
            }
        },
        error: function() {
            layer.msg('系统繁忙');
        }
    });
});

function a() {
    var height1 = $(window).height();
    var height2 = $('.main_info_oder').height();
    if(height2 >= (height1 - 127)) {
        $('.order').show();
    } else {
        $('.order').hide();
    }
}



function timeF(t) {
    var arr = [];
    t = parseInt(t);
    switch(t) {
        case 1:
            arr.push(7.25, 8);
            break;
        case 2:
            arr.push(8 + 35 / 60, 9 + 1 / 3);
            break;
        case 3:
            arr.push(9 + 55 / 60, 10 + 40 / 60);
            break;
        case 4:
            arr.push(11.25, 12);
            break;
        case 5:
            arr.push(12 + 35 / 60, 13 + 1 / 3);
            break;
        case 6:
            arr.push(13 + 55 / 60, 14 + 2 / 3);
            break;
        case 7:
            arr.push(15.25, 16);
            break;
        case 8:
            arr.push(16 + 35 / 60, 17 + 1 / 3);
            break;
        case 9:
            arr.push(17 + 55 / 60, 18 + 2 / 3);
            break;
            }
    return arr;
}

function cancel(order_id, date, time_id) {
    var b = timeF(time_id);
    if(date > b[1]) {
        location.reload();
    }
    localStorage.order_id = order_id;
    window.location.href = '../report/student_cancel_reason.html';

}

function doing(date, time_id) {
    var b = timeF(time_id);
    if(date > b[1]) {
        location.reload();
    }
}

function com(order_id) {
    localStorage.order_id = order_id;
    window.location.href = '../comments/comments.html';
}


/*监听手机返回按键*/
document.addEventListener("plusready", function() {
    // 注册返回按键事件
    plus.key.addEventListener('backbutton', function() {
        // 事件处理
        plus.nativeUI.confirm("退出程序？", function(event) {
            if(event.index) {
                plus.runtime.quit();
            }
        }, null, ["取消", "确定"]);
    }, false);3
});



/*页面加载判断是否有评价如果没有评价直接跳转到评价*/
function no_pingjia(res){
	localStorage.pingjiaorderid = res;
	window.location.href= "../comments/comments.html"

}
